//
//  HomePageController.m
//  BCNADemo
//
//  Created by 马东朝 on 2018/5/16.
//  Copyright © 2018年 马东朝. All rights reserved.
//

#import "HomePageController.h"

@interface HomePageController (){
    UIImageView *circleview;
    CircleProView *proView;
    PicLabels *linkSpace;
    PicLabels *linkLost;
    PicLabels *linkImprove;
    NSTimer *timer;
    UILabel *label;
    UIButton *button;
    UIView *successLink;
    UIImageView *bgImageView2;
    UILabel *alable;
}
@property (nonatomic, strong) HKPieChartView *pieChartView;
@property (nonatomic, strong)JDradualLoadingView *colorLoading;
@property (nonatomic,strong)FirstWaves *firstWare;

@property (nonatomic,strong)SecondWaves *secondWare;

@end
static int timeNum = 0;
@implementation HomePageController
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = NO;
    //设置导航栏背景图片为一个空的image，这样就透明了
    [self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
    //去掉透明后导航栏下边的黑边
    [self.navigationController.navigationBar setShadowImage:[[UIImage alloc] init]];
    timer= [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(updateTime) userInfo:nil repeats:YES]; self.navigationController.navigationBar.titleTextAttributes=
    @{NSForegroundColorAttributeName:[UIColor colorWithHexString:@"#9194a6"],
      NSFontAttributeName:[UIFont systemFontOfSize:16]};
} 
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor colorWithHexString:@"#12121f"];
    self.titleLabel.text = @"GOLINK";
    self.but.hidden = YES;
    
    
    [self.view addSubview:self.colorLoading];
    
    
    [self addcircleSubVeiws];
    [self addLinkDataLabels];
    [self addBtn];
    [self pieChartView];
}
- (void)addcircleSubVeiws{
    CGFloat width = kWidth(865);
    circleview = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, width, width)];
    circleview.center = CGPointMake(ScreenWidth/2, kHeight(170) + width/2 + 64);
    [self.view addSubview:circleview];
    circleview.image = [UIImage imageNamed:@"yaunhuan2@3x.png"];
    if (_colorLoading) {
        _colorLoading.center =  CGPointMake(ScreenWidth/2, kHeight(170) + width/2 + 64);
        _colorLoading.bounds =  CGRectMake(0, 0, width, width);
    }
    
    UIImageView *bgImageView  = [[UIImageView alloc] initWithFrame:CGRectMake(100, 200, kWidth(695), kWidth(695))];
    [self.view addSubview:bgImageView];
    bgImageView.layer.cornerRadius = kWidth(695)/2;
    bgImageView.layer.masksToBounds = YES;
    bgImageView.backgroundColor = [UIColor colorWithHexString:@"#12121f"];
    bgImageView.center = CGPointMake(_colorLoading.center.x, _colorLoading.center.y);
    //第一个波浪
    self.firstWare = [[FirstWaves alloc]initWithFrame:CGRectMake(0, 0, kWidth(695), kWidth(695))];
    
    self.firstWare.alpha=0.9;
    
    //第二个波浪
    self.secondWare = [[SecondWaves alloc]initWithFrame:CGRectMake(0, 0, kWidth(695), kWidth(695))];
    
    self.secondWare.alpha=0.7;
    
    [bgImageView addSubview:self.firstWare];
    
    [bgImageView addSubview:self.secondWare];
    bgImageView2 = [[UIImageView alloc] initWithFrame:CGRectMake(100, 200, kWidth(695), kWidth(695))];
    [self.view addSubview:bgImageView2];
    bgImageView2.center = CGPointMake(bgImageView.center.x, bgImageView.center.y);
    bgImageView2.layer.cornerRadius = 100;
    bgImageView2.hidden = YES;
    bgImageView2.layer.masksToBounds = YES;
    bgImageView2.image = [UIImage imageNamed:@"success@3x.png"];
    
    label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 20)];
    label.center = CGPointMake( kWidth(695)/2,  kWidth(695) * 0.35);
    label.textAlignment = NSTextAlignmentCenter;
    label.font = [UIFont systemFontOfSize:kHeight(46)];
    label.textColor = [UIColor colorWithHexString:@"#9194a6"];
    label.text = @"未开启加速";
    [bgImageView addSubview:label];
    [self addLinkSuccessWithSupView:bgImageView];
    
    
    proView = [[CircleProView alloc] initWithFrame:CGRectMake(0, 0, width-1, width-1)];
    [self.view addSubview:proView];
    proView.center = CGPointMake(_colorLoading.center.x, _colorLoading.center.y);
    //    proView.progress = 0.6;
    
    proView.hidden = YES;
    
    
}
- (void)addLinkSuccessWithSupView:(UIView *)superView{
    successLink = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kWidth(695), kWidth(695)/2)];
    UIImageView *imageView  = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, kWidth(210), kWidth(136))];
    imageView.image = [UIImage imageNamed:@"jiasuchenggong@3x.png"];
    imageView.center = CGPointMake(kWidth(695)/2, kWidth(695)/4);
    alable = [[UILabel alloc] initWithFrame:CGRectMake(kWidth(15), CGRectGetMaxY(imageView.frame) + kHeight(30), kHeight(695-30), kHeight(100))];
    alable.text = @"已加速 00:00:00";
    alable.textColor = [UIColor colorWithHexString:@"#9194a6"];
    alable.font = [UIFont systemFontOfSize:kHeight(44)];
    alable.textAlignment = NSTextAlignmentCenter;
    [successLink addSubview:imageView];
    [successLink addSubview:alable];
    [superView addSubview:successLink];
    
    successLink.hidden = YES;
    
}
- (void)updateTime{
    timeNum ++;
    alable.text =  [NSString stringWithFormat:@"已加速 %02d:%02d:%02d",timeNum %(3600*12)/3600,timeNum%3600/60,timeNum%60];
}
- (void)addLinkDataLabels{
    linkSpace = [[PicLabels alloc] initWithFrame:CGRectMake(kWidth(140), CGRectGetMaxY(circleview.frame) + kHeight(210), kWidth(280), kHeight(42))];
    [self.view addSubview:linkSpace];
    NSInteger aaa = 37;
    linkSpace.pictures.image = [UIImage imageNamed:@"ping@3x.png"];
    linkSpace.textLable.text = [NSString stringWithFormat:@"Ping %ldms", aaa];
    
    linkLost = [[PicLabels alloc] initWithFrame:CGRectMake(0, 0, kWidth(240), kHeight(42))];
    [self.view addSubview:linkLost];
    linkLost.center = CGPointMake(ScreenWidth/2, linkSpace.center.y);
    NSInteger bbb = 37;
    linkLost.pictures.image = [UIImage imageNamed:@"loss@3x.png"];
    linkLost.textLable.text = [NSString stringWithFormat:@"丢包 %ld%%", bbb];
    
    UIView *linView1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1, kHeight(48))];
    linView1.center = CGPointMake(CGRectGetMinX(linkLost.frame) - kWidth(46), linkLost.center.y);
    UIView *linView2 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1, kHeight(48))];
    linView2.center = CGPointMake(CGRectGetMaxX(linkLost.frame) + kWidth(46), linkLost.center.y);
    
    linView1.backgroundColor = [UIColor colorWithHexString:@"#9194a6"];
    linView2.backgroundColor = [UIColor colorWithHexString:@"#9194a6"];
    [self.view addSubview:linView1];
    [self.view addSubview:linView2];
    
    
    
    
    linkImprove = [[PicLabels alloc] initWithFrame:CGRectMake(0, 0, kWidth(320), kHeight(42))];
    [self.view addSubview:linkImprove];
    NSInteger ccc = 37;
    linkImprove.center = CGPointMake(CGRectGetMaxX(linView2.frame) + kWidth(160)+kWidth(46) , CGRectGetMaxY(circleview.frame) + kHeight(210) + kHeight(21));
    linkImprove.pictures.image = [UIImage imageNamed:@"tisheng@3x.png"];
    linkImprove.textLable.text = [NSString stringWithFormat:@"综合提升 %ld%%", ccc];
}
- (void)addBtn{
    button= [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0, 0, kWidth(680), kHeight(152));
    button.layer.cornerRadius = kHeight(152)/2;
    button.center = CGPointMake(ScreenWidth/2, CGRectGetMaxY(linkImprove.frame) + kHeight(166) + kHeight(152)/2);
    button.layer.masksToBounds = YES;
    [self.view addSubview:button];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    button.backgroundColor = [UIColor colorWithHexString:@"#56bdf8"];
    [button setTitle:@"一键加速" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(handleBUtton:) forControlEvents:UIControlEventTouchUpInside];
}
-(JDradualLoadingView*)colorLoading {
    if (!_colorLoading) {
        _colorLoading = [[JDradualLoadingView alloc] init];
        _colorLoading.backgroundColor = [UIColor clearColor];
        _colorLoading.lineColor = [UIColor cyanColor];
        _colorLoading.lineWidth = 3;
        _colorLoading.hidden = NO;
    }
    return _colorLoading;
}
- (void)animateWave{
    
    
    [UIView animateWithDuration:1 animations:^{
        
        self.firstWare.transform = CGAffineTransformMakeTranslation(0, 20);
        
        self.secondWare.transform = CGAffineTransformMakeTranslation(0, 20);
        
    } completion:^(BOOL finished) {
        
        [UIView animateWithDuration:1 animations:^{
            
            self.firstWare.transform = CGAffineTransformMakeTranslation(0, 0);
            
            self.secondWare.transform = CGAffineTransformMakeTranslation(0, 0);
            
        } completion:nil];
        
    }];
    
}
#pragma mark -- BUtton Action
- (void)handleBUtton:(UIButton *)sender{
    
    NSString *isLogin = [[NSUserDefaults standardUserDefaults] valueForKey:ISLogin];
    if (![isLogin isEqualToString:@"YES"]) {
        [self.navigationController pushViewController:[[LRChoiceController alloc]init] animated:YES];
        self.tabBarController.tabBar.hidden = YES;
    }else{
        
        NSString *title = sender.titleLabel.text;
        if ([title isEqualToString:@"一键加速"]) {
            
            [_colorLoading startAnimation];
            [self.firstWare startAnimal];
            [self.secondWare startAnimal];
            [sender setTitle:@"连接中..." forState:UIControlStateNormal];
            
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [self.firstWare stop];
                [self.secondWare stop];
                [_colorLoading stopAnimation];
                label.hidden = YES;
                proView.hidden = NO;
                _colorLoading.hidden = YES;
                //            proView.progress = 1.0;
                _pieChartView.hidden = NO;
                [_pieChartView updatePercent:89 animation:YES];
                //button.backgroundColor = [UIColor colorWithHexString:@"#fo81a8"];
                successLink.hidden = NO;
                bgImageView2.hidden = NO;
                sender.backgroundColor = RGB(240, 129, 168, 1.0);
                [sender setTitle:@"停止加速" forState:UIControlStateNormal];
                [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
            });
        }
        
        if ([title isEqualToString:@"停止加速"]) {
            [timer invalidate];
            sender.backgroundColor = [UIColor colorWithHexString:@"#56bdf8"];
            label.text = @"未开启加速";
            label.hidden = NO;
            _colorLoading.hidden = NO;
            bgImageView2.hidden = YES;
            proView.hidden = YES;
            successLink.hidden = YES;
            _pieChartView.hidden = YES;
            [sender setTitle:@"一键加速" forState:UIControlStateNormal];
        }
    }
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [timer invalidate];
    
    timer = nil;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (HKPieChartView *)pieChartView {
    if (!_pieChartView) {
        CGFloat width = kWidth(865);
        
        _pieChartView = [[HKPieChartView alloc]initWithFrame:CGRectMake((self.view.frame.size.width - width)/2, 150, width, width)];
        _pieChartView.center = CGPointMake(_colorLoading.center.x, _colorLoading.center.y);
        [self.view addSubview:_pieChartView];
        _pieChartView.hidden = YES;
    }
    return _pieChartView;
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
